package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.mapper.ComponentBrandMapper;
import com.carkeeper.pojo.ComponentBrandPO;
import com.carkeeper.service.ComponentBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ComponentBrandImpl implements ComponentBrandService {
    @Autowired
    private ComponentBrandMapper componentBrandMapper;

    @Override
    public void create(ComponentBrandPO componentBrandPO) {
        componentBrandMapper.insert(componentBrandPO);
    }

    @Override
    public void update(Long id, ComponentBrandPO componentBrandPO) {
        QueryWrapper<ComponentBrandPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        componentBrandMapper.update(componentBrandPO, queryWrapper);
    }

    @Override
    public void delete(long componentBrandId) {
        componentBrandMapper.deleteById(componentBrandId);
    }

    @Override
    public ComponentBrandPO query(Long id) {
        ComponentBrandPO componentBrandPO = componentBrandMapper.selectById(id);
        return componentBrandPO;
    }

    @Override
    public Page<ComponentBrandPO> query(String brandName, String code, Long page, Long pageSize) {
        Page<ComponentBrandPO> pageResult = new Page<>(page, pageSize);
        QueryWrapper<ComponentBrandPO> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(brandName)) {
            wrapper.like("brand_name", brandName);
        }
        if (!StringUtils.isEmpty(code)) {
            wrapper.like("code", code);
        }
        wrapper.orderByAsc("sort");
        return componentBrandMapper.selectPage(pageResult, wrapper);
    }
}
